//
//  ViewController.m
//  LuaDemo
//
//  Created by wesleyxiao on 16/2/3.
//  Copyright © 2016年 wesleyxiao. All rights reserved.
//

#import "ViewController.h"
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"

int cubeTarget_position(lua_State *L){
    lua_pushnumber(L, 123.123);
    lua_pushnumber(L, 456.456);
    return 2;
}

int go_right(lua_State *L){
    UIView *sc = (__bridge UIView *)(lua_touserdata(L, 1));
    sc.center = CGPointMake(sc.center.x+10, sc.center.y);
    return 0;
}

int go_left(lua_State *L){
    UIView *sc = (__bridge UIView *)(lua_touserdata(L, 1));
    sc.center = CGPointMake(sc.center.x-10, sc.center.y);
    return 0;
}

int go_up(lua_State *L){
    UIView *sc = (__bridge UIView *)(lua_touserdata(L, 1));
    sc.center = CGPointMake(sc.center.x, sc.center.y-10);
    return 0;
}

int go_down(lua_State *L){
    UIView *sc = (__bridge UIView *)(lua_touserdata(L, 1));
    sc.center = CGPointMake(sc.center.x, sc.center.y+10);
    return 0;
}

int get_cube_position(lua_State *L){
    UIView *sc = (__bridge UIView *)lua_touserdata(L, 1);
    lua_pushnumber(L, sc.center.x);
    lua_pushnumber(L, sc.center.y);
    return 2;
}

const struct luaL_Reg cubeLib[] = {
    {"cubeP", cubeTarget_position},
    {"go_right", go_right},
    {"go_left", go_left},
    {"go_up", go_up},
    {"go_down", go_down},
    {"get_cube_Position",get_cube_position},

    {NULL, NULL}
};

static int luaopen_cubeLib (lua_State *L){
    luaL_register(L, "myLib", cubeLib);
    
    return 1;
}


@interface ViewController ()
{
    lua_State *L;
    UIView * otherCube;
}
@end

@implementation ViewController
- (void)initLuaState
{
    L = luaL_newstate();
    luaL_openlibs(L);
    lua_settop(L, 0);
    luaopen_cubeLib(L);
    
    int err;
    
    NSString *luaFilePath = [[NSBundle mainBundle] pathForResource:@"Script" ofType:@"lua"];
    err = luaL_loadfile(L, [luaFilePath cStringUsingEncoding:[NSString defaultCStringEncoding]]);

    
    if (0 != err) {
        luaL_error(L, "compile error: %s",lua_tostring(L, -1));
        return;
    }
    
    err = lua_pcall(L, 0, 0, 0);
    if (0 != err) {
        luaL_error(L, "run error: %s",lua_tostring(L, -1));
        return;
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    otherCube = [[UIView alloc]initWithFrame:CGRectMake(100, 200, 20, 20)];
    [otherCube setBackgroundColor:[UIColor greenColor]];
    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
    [otherCube addGestureRecognizer:panGesture];
    
    [self.view addSubview:otherCube];
    
    [self initLuaState];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//- (void)runLoop:(id)sender
//{
//   
//    lua_getglobal(L, "print_cube_position");
//    
//    int err = lua_pcall(L, 0, 0, 0);
//    if (0 != err) {
//        luaL_error(L, "run error: %s",
//                   lua_tostring(L, -1));
//        return;
//    }
//    
//}
@end
